package org.as.sdg.beans;

import java.util.HashMap;

import org.as.sdg.builder.ApplicationBuilder;
import org.as.sdg.util.FacesUtils;

@SuppressWarnings("serial")
public class NavigationPanelCollapsibleFactory extends HashMap<String, Boolean> {

	/**
	 * <p>
	 * Gets panelCollapsible expanded state associated with this user session
	 * given the parentNodeId which must be a valid parent node id. The nodes
	 * are as defined in the application meta data.
	 * </p>
	 * <p>
	 * Once the tree is build it is added to the HashMap and the cached version
	 * is loaded on the next request.
	 * </p>
	 * 
	 * @param nodeId
	 *            nodeId of the meta data node in which we want to build
	 *            panelCollapsible panel with.
	 * @return tree true if the panelCollapsible is expanded, otherewise; false.
	 */
	public Boolean get(Object nodeId) {

		// check to see if the given tree model has been constructed.
		Boolean panelColapsibleState = super.get(nodeId);

		// if not we build it and add it to our hash.
		if (panelColapsibleState == null) {

			// uses application builder to get us a Node for the given node id.
			ApplicationBuilder applicationBuilder = (ApplicationBuilder) FacesUtils
					.getManagedBean("applicationBuilder");

			panelColapsibleState = applicationBuilder.getNode((String) nodeId)
					.isExpanded();

			// cach the result for future reference
			this.put((String) nodeId, panelColapsibleState);
		}

		// finally return our node
		return panelColapsibleState;
	}
}